package Day10_Ex3_queues1;

//**********************************************************************
// This class implements an object representing elapsed time.
// It supports formats of time that include a decimal representation,
// and hours, minutes, and seconds. It provides operations to add and
// subtract times. Negative times are not formatted properly by toString
//**********************************************************************

public class Time
{
  double seconds;                  // Variable to hold seconds

  // Constructors for class Time

  public Time()
  {
    seconds = 0.0;
  }

  public Time(double newSeconds)
  {
    seconds = newSeconds;
  }

  public Time(int hours, int minutes, double newSeconds)
  {
    seconds = (double)(hours * 3600 + minutes * 60) + newSeconds;
  }

  // Observers for class Time

  public double getTime()
  {
    return seconds;                             // Return elapsed time
  }

  public int getHours()
  {
    return (int)seconds / 3600;                 // Compute whole hours from seconds
  }

  public int getMinutes()
  {
    int remainingSeconds = (int)seconds % 3600; // Seconds after hours taken out
    return remainingSeconds / 60;               // Compute minutes from remainder
  }

  public double getSeconds()
  {
    return seconds % 60.0;                      // Seconds after minutes taken out
  }

  public String toString()                      // Returns HH:MM:SS.FFF
  {
    int hours = (int)seconds / 3600;
    int minutes = (int)seconds % 3600 / 60;
    return  hours + ":" + minutes + ":" + seconds % 60; 
  }

  // Operations for class Time

  public Time plus(Time otherTime)
  {
    return new Time(seconds + otherTime.seconds);
  }

  public Time minus(Time otherTime)
  {
    return new Time(seconds - otherTime.seconds);
  }
  
  public int compareTo(Time other)
  {
    int roundSeconds = (int)(seconds * 100.0 + 0.5);       // Round up seconds * 100
    int roundOther = (int)(other.getTime() * 100.0 + 0.5); // Round up other * 100
    if (roundSeconds > roundOther)                         // If this object > other
      return 1;                                            //   then positive result
    else if (roundSeconds < roundOther)                    // If this object < other
      return -1;                                           //   then negative result
    else                                                   // If this object = other
      return 0;                                            //   then zero result
  }

}
